package com.yongz.service.impl;

import com.github.davidfantasy.jwtshiro.JWTUserAuthService;
import com.github.davidfantasy.jwtshiro.UserInfo;
import com.yongz.biz.StuBiz;
import com.yongz.vo.StuVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
@RequiredArgsConstructor
public class JWTStuAuthServiceImpl implements JWTUserAuthService {
    @Autowired
    private StuBiz stuBiz;
    private final StringRedisTemplate stringRedisTemplate;


    @Override
    public UserInfo getUserInfo(String username) {
        StuVo stuVoWithRedis = stuBiz.getStuVoWithUsername(username);
        UserInfo userInfo = new UserInfo();
        if (null == stuVoWithRedis.getId()){
            return null;
        }

        userInfo.setAccount(stuVoWithRedis.getUsername());
        userInfo.setSecret(stuVoWithRedis.getPassword());
        return userInfo;
    }

    public void onAuthenticationFailed(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        httpServletResponse.setStatus(401);
    }

    @Override
    public void onAuthorizationFailed(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        httpServletResponse.setStatus(403);
    }
}
